<?php
/*
 * Created on Dec 27, 2007
 *
 * To change the template for this generated file go to
 * Window - Preferences - PHPeclipse - PHP - Code Templates
 */

require_once "../../../../mainfile.php";
require_once('MOD_GLOBALS.php');


switch ($_POST['action']) {

case "newCat":
    newCategory();
    break;

case "getCat":
	getCat();
	break;

case "updateCat":
	updateCat();
	break;

case "deleteCat":
	deleteCat();
	break;

case "deletEvent":
	deletEvent();
	break;

case "approveEvent":
	approveEvent();
	break;


case "newEvent":
	newEvent();
	break;

case "addMediaNotif":
	addMediaNotif();
	break;

case "getMediaEntities":
	getMediaEntities();
	break;

case "updateMediaEntity":
	updateMediaEntity();
	break;

case "deleteMediaEntity":
	deleteMediaEntity();
	break;

case "fetchEvent":
	fetchEvent($_POST['ID']);
	break;

case "updateEvent":
	updateEvent();
	break;
}



//-----------------------------------------------
//SEND EVENT TO ADDITIONAL MEDIA OUTLETS AS WELL
//-----------------------------------------------
function syndicateEvent($ID){
	global $xoopsDB;
	global $xoopsModuleConfig;

	//SELECT EVENT INFORMATION TO INCLUDE IN EMAIL TO MEDIA OUTLETS
	if($ID==""){
		//FROM ADMIN EVENT SUBMISSIONS WHERE THE ID IS UNKOWN
		$result_e = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("calendar_events")." ORDER BY ID DESC LIMIT 1") ;
		if ( $result_e ) {
			$myrowEVT = $xoopsDB->fetchArray($result_e);
		}

	}else{
		//FROM ADMIN APPROVAL ACTION
		$result_e = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("calendar_events")." WHERE ID = '$ID'") ;
		if ( $result_e ) {
			$myrowEVT = $xoopsDB->fetchArray($result_e);
		}

	}

		//RETRIEVE MEDIA OUTLET INFO AND EMAIL EVENT INFO TO EACH ONE
		$result = $xoopsDB->query("SELECT MediaName, email FROM ".$xoopsDB->prefix("calendar_mednot")." WHERE MediaName != 'Default'") ;
		if ( $result ) {

			require_once XOOPS_ROOT_PATH."/class/xoopsmailer.php";
			while($myrow = $xoopsDB->fetchArray($result)){
				$xoopsMailer =& getMailer();
				$xoopsMailer->useMail();
				$xoopsMailer->setTemplateDir(XOOPS_ROOT_PATH.'/modules/calendar/templates');
				$xoopsMailer->setTemplate("mailRecipient.tpl");
				$xoopsMailer->setToEmails($myrow['email']);
				$xoopsMailer->setFromEmail('noReply@'.strtolower($xoopsModuleConfig['stationLetters']).'.org');//$_POST['FROM']
				$xoopsMailer->setFromName($xoopsModuleConfig['stationLetters']);
				$xoopsMailer->setSubject($xoopsModuleConfig['stationLetters']. " EVENT CALENDAR SUBMISSION");

				if($myrowEVT['EVT_TS_END'] == 0){
					$endTime = "Not Specified (one day event)";
				}else{
					$endTime = date("M j, Y  h:i a",$myrowEVT['EVT_TS_END']);
				}
				$xoopsMailer->assign("MESSAGE","\n EVENT TITLE: ".$myrowEVT['EVT_TITLE']." \n START: ".date("M j, Y  h:i a",$myrowEVT['EVT_TS'])." \n END: $endTime \n DESCRIPTION: ".$myrowEVT['EVT_DSCR']." \n LOCATION: ".$myrowEVT['EVT_LOC'] ." \n CONTACT NAME: ".$myrowEVT['CON_NAME']." \n CONTACT NUMBER: ".$myrowEVT['CON_NUM']." \n CONTACT EMAIL: ".$myrowEVT['EMAIL']." \n EVENT WEBSITE: ".$myrowEVT['URL']);
				$xoopsMailer->send();
				++$i;
			}


		}



}


//-----------------------------------------------------------------------------------------------

function newCategory(){

	global $xoopsDB;
	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("insert into ".$xoopsDB->prefix("calendar_evnt_category")." values ('0','".$_POST['name']."','".$_POST['description']."',CURRENT_TIMESTAMP())");
		if ( $result ) {

			$rows = $xoopsDB->getAffectedRows($result);
			if($rows == 1){
				echo "/*{success:'true',name:'".$_POST['name']."'}*/";
			}
		}
	}
}

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function getCat(){
	global $xoopsDB;

	$result = $xoopsDB->query("select * from ".$xoopsDB->prefix("calendar_evnt_category")." WHERE ID = '".$_POST['catID']."'") ;
	if ( $result ) {
		$myrow = $xoopsDB->fetchArray($result);
		echo '/*{success:"true",catName:"'.$myrow['NAME'].'",catDesc:"'.str_replace("\n","",$myrow['DESCR_LONG']).'"}*/';

	}


}

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function updateCat(){
	global $xoopsDB;
	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("update ".$xoopsDB->prefix("calendar_evnt_category")." SET NAME='".$_POST['name']."', DESCR_LONG='".$_POST['description']."' WHERE ID = '".$_POST['catID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';

		}
	}

}

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function deleteCat(){
	global $xoopsDB;
	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("calendar_evnt_category")." WHERE ID = '".$_POST['catID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
		}

	}


}

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function newEvent(){
	global $xoopsDB;

	//###########################
	//start time
	$date = explode('/',$_POST['date1']);
	$time = explode(':',substr($_POST['time1'], 0, -3));
	$AMPM = substr($_POST['time1'],-2);
	if($AMPM == 'PM' && $time[0] != 12){
		$time[0]+= 12;
	}else if($AMPM == 'AM' && $time[0] == 12){
		$time[0] = 0;
	}




	$timeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);

	//###########################
	//end time

	//if an end time has been set but no end date it is likely a one day only event
//	if($_POST['time2']!= ""){
//
//		if($_POST['date2'] != ""){
//			$date = explode('/',$_POST['date2']);
//		}else{
//			$date = explode('/',$_POST['date1']);
//		}
//
//		$time = explode(':',substr($_POST['time2'], 0, -3));
//		$AMPM = substr($_POST['time2'],-2);
//		if($AMPM == 'PM'){
//			$time[0]+= 12;
//		}
//		$endTimeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);
//
//	//multi day event with no end time specified so use end time from start info
//	}else if($_POST['time2']== "" && $_POST['date2'] != ""){
//
//		$date = explode('/',$_POST['date2']);
//		$time = explode(':',substr($_POST['time1'], 0, -3));
//		$AMPM = substr($_POST['time1'],-2);
//		if($AMPM == 'PM'){
//			$time[0]+= 12;
//		}
//		$endTimeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);
//
//	}else{
		$endTimeStamp = null;
//	}




	if($_POST['website']=="http://"){
		$url = "";
	}else{
		$url = $_POST['website'];
	}

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$FL_APPRV = 'Y';
	}else{
		$FL_APPRV = 'N';
	}
	$result = $xoopsDB->query("insert into ".$xoopsDB->prefix("calendar_events")." VALUES ('0','$FL_APPRV','".$timeStamp."','".$endTimeStamp."','1','".$_POST['eventCatAdd']."','".htmlspecialchars($_POST['title'],ENT_QUOTES)."','".htmlspecialchars($_POST['description'],ENT_QUOTES)."','".htmlspecialchars($_POST['location'],ENT_QUOTES)."','".htmlspecialchars($_POST['contact_name'],ENT_QUOTES)."','".htmlspecialchars($_POST['contact_phone'],ENT_QUOTES)."','".htmlspecialchars($_POST['contact_email'],ENT_QUOTES)."','".htmlspecialchars($url,ENT_QUOTES)."')");

	if ( $result ) {
		$rows = $xoopsDB->getAffectedRows($result);
		if($rows == 1){
			echo "/*{success:'true',event:'".htmlspecialchars($_POST['title'],ENT_QUOTES)."'}*/";

			//SEND EVENT TO SELECTED MEDIA ENTITIES IF AN ADMIN SUBMISSION WHICH DOESN'T NEED APPROVAL
			if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
				syndicateEvent("");
			}

		}else{
			//error message
			echo "/*{success:'false',event:'".htmlspecialchars($_POST['title'],ENT_QUOTES)."'}*/";

		}
	}
}
//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function deletEvent(){
	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("DELETE FROM ".$xoopsDB->prefix("calendar_events")." WHERE ID = '".$_POST['ID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
		}
	}

}

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function approveEvent(){
	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("update ".$xoopsDB->prefix("calendar_events")." SET FL_APPRV = 'Y' WHERE ID = '".$_POST['ID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
			syndicateEvent($_POST['ID']);
		}
	}//end if

}//end function

//-----------------------------------------------------------------------------------------------

//-----------------------------------------------------------------------------------------------

function addMediaNotif(){

	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("insert into ".$xoopsDB->prefix("calendar_mednot")." (ID, MediaName, email) values (0,'".$_POST['name']."','".$_POST['email']."')") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
		}
	}


}


//-----------------------------------------------------------------------------------------------
// Retrieve selected media entity and pass it back to the form for edit or delete functions
//-----------------------------------------------------------------------------------------------
function getMediaEntities(){

	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("select MediaName, email from ".$xoopsDB->prefix("calendar_mednot")." where ID = '".$_POST['notID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true",MediaName:"'.$myrow['MediaName'].'",email:"'.$myrow['email'].'"}*/';
		}
	}


}



//-----------------------------------------------------------------------------------------------
// Update selected media entity with new name or email address from end user
//-----------------------------------------------------------------------------------------------
function updateMediaEntity(){

	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("update ".$xoopsDB->prefix("calendar_mednot")." set MediaName = '".$_POST['MediaName']."', email = '".$_POST['email']."' where ID = '".$_POST['notID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
		}
	}


}


//-----------------------------------------------------------------------------------------------
// Delete selected media entity
//-----------------------------------------------------------------------------------------------
function deleteMediaEntity(){

	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){
		$result = $xoopsDB->query("delete from  ".$xoopsDB->prefix("calendar_mednot")." where ID = '".$_POST['notID']."'") ;
		if ( $result ) {
			$myrow = $xoopsDB->fetchArray($result);
			echo '/*{success:"true"}*/';
		}
	}


}


//-----------------------------------------------------------------------------------------------
// used to return a json array used in the event edit tab
//-----------------------------------------------------------------------------------------------
function fetchEvent($ID){

		//used in updateEvent();
		$_SESSION['m_ID']= $ID;

		global $xoopsDB;

		if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){

			$result = $xoopsDB->query("SELECT * FROM ".$xoopsDB->prefix("calendar_events")." WHERE ID = " . $ID) ;
			if ( $result ) {
				$myrow = $xoopsDB->fetchArray($result);
				echo '/*{success:"true",s_date:"'.date("m/d/Y",$myrow['EVT_TS']).'",s_time:"'.date("h:i A",$myrow['EVT_TS']).'",e_date:"'.date("m/d/Y",$myrow['EVT_TS_END']).'",e_time:"'.date("h:i A",$myrow['EVT_TS_END']).'",title:"'.trim($myrow['EVT_TITLE']).'",EVT_CAT:"'.$myrow['EVT_CAT'].'",EVT_DSCR:"'.trim($myrow['EVT_DSCR']).'",EVT_LOC:"'.trim($myrow['EVT_LOC']).'",CON_NAME:"'.trim($myrow['CON_NAME']).'",CON_NUM:"'.trim($myrow['CON_NUM']).'",EMAIL:"'.trim($myrow['EMAIL']).'",URL:"'.trim($myrow['URL']).'"}*/';
			}
		}

}


//-----------------------------------------------------------------------------------------------
// UPDATE EVENT INFO
//-----------------------------------------------------------------------------------------------
function updateEvent(){

	global $xoopsDB;

	if(isset($_SESSION['XOOPS_ISADMIN']) && $_SESSION['XOOPS_ISADMIN'] == 1){


//###########################
	//start time
	$date = explode('/',$_POST['e_date1']);
	$time = explode(':',substr($_POST['e_time1'], 0, -3));
	$AMPM = substr($_POST['e_time1'],-2);
	if($AMPM == 'PM' && $time[0] != 12){
		$time[0]+= 12;
	}else if($AMPM == 'AM' && $time[0] == 12){
		$time[0] = 0;
	}


	$timeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);

	//###########################
	//end time

	//if an end time has been set but no end date it is likely a one day only event
//	if($_POST['time2']!= ""){
//
//		if($_POST['date2'] != ""){
//			$date = explode('/',$_POST['date2']);
//		}else{
//			$date = explode('/',$_POST['date1']);
//		}
//
//		$time = explode(':',substr($_POST['time2'], 0, -3));
//		$AMPM = substr($_POST['time2'],-2);
//		if($AMPM == 'PM'){
//			$time[0]+= 12;
//		}
//		$endTimeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);
//
//	//multi day event with no end time specified so use end time from start info
//	}else if($_POST['time2']== "" && $_POST['date2'] != ""){
//
//		$date = explode('/',$_POST['date2']);
//		$time = explode(':',substr($_POST['time1'], 0, -3));
//		$AMPM = substr($_POST['time1'],-2);
//		if($AMPM == 'PM'){
//			$time[0]+= 12;
//		}
//		$endTimeStamp = mktime($time[0],$time[1],'00',$date[0],$date[1],$date[2]);
//
//	}else{
		$endTimeStamp = null;
//	}




	if($_POST['e_website']=="http://"){
		$url = "";
	}else{
		$url = $_POST['e_website'];
	}

			$result = $xoopsDB->query("UPDATE ".$xoopsDB->prefix("calendar_events")." set EVT_TS = '".$timeStamp."', EVT_TS_END = '".$endTimeStamp."',EVT_CAT='".$_POST['e_eventCatAdd']."',EVT_TITLE='".htmlspecialchars(trim($_POST['e_title']),ENT_QUOTES)."',EVT_DSCR='".htmlspecialchars(trim($_POST['e_description']),ENT_QUOTES)."',EVT_LOC='".htmlspecialchars(trim($_POST['e_location']),ENT_QUOTES)."',CON_NAME='".htmlspecialchars(trim($_POST['e_contact_name']),ENT_QUOTES)."',CON_NUM='".$_POST['e_contact_phone']."',EMAIL='".$_POST['e_contact_email']."',URL='".$url."' WHERE ID = " . $_SESSION['m_ID']) ;
			if ( $result ) {
				$myrow = $xoopsDB->fetchArray($result);
				echo '/*{success:"true",title:"'.htmlspecialchars(trim($_POST['e_title']),ENT_QUOTES).'"}*/';
			}
		}

}


?>
